* Table 83
* Survival logit regression, whites
qui{
clear 
use "${data}/HRS_Analysis.dta"
*keep alive and dead: 1 and 4 are alive and 5 and 6 are dead
keep if iwstatr==1 | iwstatr==4 | iwstatr==5  | iwstatr==6 

* keep Whites
drop if hispanic==1 // drop Hispanics
drop if race==2 // drop Blacks

rename iwendmr month // interview end date month

* INDICATOR FOR DEATH IN NEXT YEAR
gen indicator_death=.
sort ID year
replace indicator_death=0 if  (iwstatr==1 | iwstatr==4) & radyear!=year+1 & radyear!=year 
replace indicator_death=1 if   (iwstatr==1 | iwstatr==4) & radyear==year // die in current year
rename radmonth month_death
replace indicator_death=1 if  (iwstatr==1 | iwstatr==4) & radyear==year+1 & month_death<=month 
drop if iwstatr==5  | iwstatr==6  //  drop observations where they are dead


gen educ_group=1 if raedyrs <=12
replace educ_group=2 if raedyrs>=13 & raedyrs<16
replace educ_group=3 if raedyrs>=16 & raedyrs!=.

gen agesq=age^2
gen agecub=age^3

keep if gender==0  // keep males 0=male, 1=women
sort year

* variable equivalent to PHYLIMS in MEPS. This is the number of ADL's the respondent has difficulty with
gen PHYLIMS=0 if ADL_combo_diff==0
replace PHYLIMS=1 if ADL_combo_diff>0 & ADL_combo_diff<10
replace PHYLIMS=2 if ADL_combo_diff>9 & ADL_combo_diff<100
replace PHYLIMS=3 if ADL_combo_diff>99 & ADL_combo_diff<1000
replace PHYLIMS=4 if ADL_combo_diff>999 & ADL_combo_diff<10000
replace PHYLIMS=5 if ADL_combo_diff>9999 & ADL_combo_diff<100000
replace PHYLIMS=6 if ADL_combo_diff>99999 & ADL_combo_diff<1000000
replace PHYLIMS=7 if ADL_combo_diff>999999 & ADL_combo_diff<10000000
rename PHYLIMS PHYLIMS1

* self reported health, same as RTHLTH1 in meps
rename health_ORIG RTHLTH1
tab RTHLTH1 // bigger score is worse
* this summary score captures mental health similar to MNHLTH1
rename health_mental MNHLTH1
tab MNHLTH1 // bigger score is worse

* indicator for getting help with an ADL
gen ADLHLP1 = 0 if ADL_combo_help==0 
replace ADLHLP1 = 1 if ADL_combo_help>0 & ADL_combo_help!=.

* same IADL
gen IADLHP1 = 0 if IADL_combo==0 
replace IADLHP1 =1  if IADL_combo>0 & IADL_combo!=.


drop if age<50


*** CONSTRUCTING H
* standardize based on distribution for ages 54 to 60

	// Standardize the variables used
	qui sum RTHLTH1 if age>53 & age<61 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen RTHLTH1_std = ( RTHLTH1 - depmean ) / depsdev


	qui sum MNHLTH1 if age>53 & age<61 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen MNHLTH1_std = ( MNHLTH1 - depmean ) / depsdev

	qui sum ADLHLP1 if age>53 & age<61 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen ADLHLP1_std = ( ADLHLP1 - depmean ) / depsdev


	qui sum IADLHP1 if age>53 & age<61 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen IADLHP1_std = ( IADLHP1 - depmean ) / depsdev


	qui sum PHYLIMS1 if age>53 & age<61 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen PHYLIMS1_std = ( PHYLIMS1 - depmean ) / depsdev

	// Factor analysis and construct continuous H 
	factor RTHLTH1_std MNHLTH1_std ADLHLP1_std IADLHP1_std PHYLIMS1_std if age>53 & age<61 [aweight=wtcrnhr], pcf factors(1)
	predict H
	drop if H==.
	
// Discretize H
tabstat H if age>53 & age<60 [aweight=wtcrnhr], stat (p50 mean p75 p90)
egen x_P =  pctile(H) if age>53 & age<61 , p(91)
qui sum x_P
scalar x_P1 = r(mean)
gen H_dis = 1 if H>=x_P1 //poor H

egen x_PP =  pctile(H) if age>53 & age<61 , p(54)
qui sum x_PP
scalar x_P2 = r(mean)
replace H_dis = 2 if H>x_P2 & H_dis!= 1 //Fair H

replace H_dis = 3 if H<=x_P2 // good
 tab H_dis [aweight=wtcrnhr] if age>=54 & age<60

 drop x_*
 drop H
 rename H_dis H

bysort educ_group: tab H
	
sort ID wave

gen divorce = 0 
replace divorce=1 if 	marital_status_indic1==0 & marital_status_indic1[_n-1]==0 & ID==ID[_n-1]
	

drop if indicator_death==.
sort ID year


drop if age>100
	fillin age educ_group marital_status_indic1 H
	replace agesq=age^2 if _fillin==1

	replace educ_group=2 if educ_group==1
	label define ed 2 "Less than College" 3 "College"
label values educ_group ed

label define H_label 1 "H=Poor" 2 "H=Fair" 3 "H=Good"
label values H H_label

label var agesq "Age sq."

label define M_label 0 "Single" 1 "Married"
label values marital_status_indic1 M_label
	
	eststo clear
logit indicator_death age agesq i.H i.educ_group i.marital_status_indic1
eststo
	esttab using "${out_tables}/Mortality_HRS.tex", varwidth(25) nogaps   compress label nobaselevels  replace  ///
	nodepvars nonumbers eqlabel(none) noconstant mtitles("Survival"  ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(5) 
	eststo clear

*tab age
predict mortality, pr
collapse mortality, by(age H educ_group marital_status_indic1)
sort H educ_group marital_status_indic1 age
drop H educ_group marital_status_indic1 age

outsheet using "${data_model}/parameters_survival.txt", nolabel nonames replace	

}

****** SURVIVAL FOR MINORITIES ******
qui{
clear 
use "${data}/HRS_Analysis.dta"
*keep alive and dead: 1 and 4 are alive and 5 and 6 are dead
keep if iwstatr==1 | iwstatr==4 | iwstatr==5  | iwstatr==6 

* races
gen race_use = 1 if hispanic!=1 & race!=2
replace race_use = 3 if hispanic==1 // hispanic
replace race_use =2 if race==2 // black

rename iwendmr month // interview end date month

gen indicator_death=.
sort ID year 
replace indicator_death=0 if  (iwstatr==1 | iwstatr==4) & radyear!=year+1 & radyear!=year 
replace indicator_death=1 if   (iwstatr==1 | iwstatr==4) & radyear==year 
rename radmonth month_death
replace indicator_death=1 if  (iwstatr==1 | iwstatr==4) & radyear==year+1 & month_death<=month 
drop if iwstatr==5  | iwstatr==6  // now drop observations where they are dead

gen educ_group=1 if raedyrs <=12
replace educ_group=2 if raedyrs>=13 & raedyrs<16
replace educ_group=3 if raedyrs>=16 & raedyrs!=.

gen agesq=age^2
gen agecub=age^3

keep if gender==0  // keep males 0=male, 1=women
sort year

* variable equivalent to PHYLIMS in MEPS. This is the number of ADL's the respondent has difficulty with
// bigger score is worse
gen PHYLIMS=0 if ADL_combo_diff==0
replace PHYLIMS=1 if ADL_combo_diff>0 & ADL_combo_diff<10
replace PHYLIMS=2 if ADL_combo_diff>9 & ADL_combo_diff<100
replace PHYLIMS=3 if ADL_combo_diff>99 & ADL_combo_diff<1000
replace PHYLIMS=4 if ADL_combo_diff>999 & ADL_combo_diff<10000
replace PHYLIMS=5 if ADL_combo_diff>9999 & ADL_combo_diff<100000
replace PHYLIMS=6 if ADL_combo_diff>99999 & ADL_combo_diff<1000000
replace PHYLIMS=7 if ADL_combo_diff>999999 & ADL_combo_diff<10000000
rename PHYLIMS PHYLIMS1

* self reported health, same as RTHLTH1 in meps
rename health_ORIG RTHLTH1
tab RTHLTH1 // bigger score is worse
* this summary score captures mental health similar to MNHLTH1
rename health_mental MNHLTH1
tab MNHLTH1 // bigger score is worse

* indicator for getting help with an ADL
gen ADLHLP1 = 0 if ADL_combo_help==0 
replace ADLHLP1 = 1 if ADL_combo_help>0 & ADL_combo_help!=.

* same IADL
gen IADLHP1 = 0 if IADL_combo==0 
replace IADLHP1 =1  if IADL_combo>0 & IADL_combo!=.

drop if age<50


*** CONSTRUCTING VARIABLES: HEALTH CAPITAL (H) *********************************
* standardize based on distribution for ages 54 to 60

	// Standardize the variables used
	qui sum RTHLTH1 if age>53 & age<61 & race_use==1 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen RTHLTH1_std = ( RTHLTH1 - depmean ) / depsdev


	qui sum MNHLTH1 if age>53 & age<61 & race_use==1 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen MNHLTH1_std = ( MNHLTH1 - depmean ) / depsdev

	qui sum ADLHLP1 if age>53 & age<61 & race_use==1 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen ADLHLP1_std = ( ADLHLP1 - depmean ) / depsdev


	qui sum IADLHP1 if age>53 & age<61 & race_use==1 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen IADLHP1_std = ( IADLHP1 - depmean ) / depsdev


	qui sum PHYLIMS1 if age>53 & age<61 & race_use==1 [aweight=wtcrnhr]
	scalar depmean = r(mean)
	scalar depsdev = r(sd)
	gen PHYLIMS1_std = ( PHYLIMS1 - depmean ) / depsdev


	factor RTHLTH1_std MNHLTH1_std ADLHLP1_std IADLHP1_std PHYLIMS1_std if age>53 & age<61 & race_use==1 [aweight=wtcrnhr], pcf factors(1)
	predict H
	

	
	drop if H==.
	
// Discretize H
tabstat  H if age>53 & age<60 [aweight=wtcrnhr], stat (p50 mean p75 p90)
egen x_P =  pctile(H) if age>53 & age<61 & race_use==1 , p(91)
qui sum x_P
scalar x_P1 = r(mean)
gen H_dis = 1 if H>=x_P1 //poor H

egen x_PP =  pctile(H) if age>53 & age<61 & race_use==1 , p(54)
qui sum x_PP
scalar x_P2 = r(mean)
replace H_dis = 2 if H>x_P2 & H_dis!= 1 //Fair H

replace H_dis = 3 if H<=x_P2 // good
tab race_use H_dis [aweight=wtcrnhr] if age>=54 & age<60, row

 
 drop x_*
 drop H
 rename H_dis H

bysort race_use educ_group: tab H
	
sort ID wave

gen divorce = 0 
replace divorce=1 if 	marital_status_indic1==0 & marital_status_indic1[_n-1]==0 & ID==ID[_n-1]
	

drop if indicator_death==.
sort ID year


drop if age>100
	fillin age educ_group  race_use marital_status_indic1 H
	replace agesq=age^2 if _fillin==1

		replace educ_group=2 if educ_group==1 // some college not significant for any race group 
		drop if educ_group==3
		
logit indicator_death age agesq i.H  i.marital_status_indic1 if race_use==1
logit indicator_death age agesq i.H  i.marital_status_indic1 if race_use==2
logit indicator_death age agesq i.H  i.marital_status_indic1 if race_use==3	
logit indicator_death age agesq i.H  i.marital_status_indic1 i.race_use
	
	

	label define ed 2 "Less than College" 3 "College"
label values educ_group ed

label define H_label 1 "H=Poor" 2 "H=Fair" 3 "H=Good"
label values H H_label

label var agesq "Age sq."

label define M_label 0 "Single" 1 "Married"
label values marital_status_indic1 M_label

label var race_use "Race"
label define race_label 1 "White" 2 "Black" 3 "Hispanic"
label values race_use race_label

	
	
logit indicator_death age agesq i.H  i.marital_status_indic1 i.race_use

	
	* the blacks are not different from whites 
	* so redo the regression and combine them. 
	replace race_use=1 if race_use==2
	
	
			eststo clear
logit indicator_death age agesq i.H  i.marital_status_indic1 i.race_use
eststo
	esttab using "${out_tables}/Mortality_HRS_races.tex", varwidth(25) nogaps   compress label nobaselevels  replace  ///
	nodepvars nonumbers eqlabel(none) noconstant mtitles("Mortality"  ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(5) 
	eststo clear


logit indicator_death age agesq i.H  i.marital_status_indic1 i.race_use
predict mortality, pr

collapse mortality, by(age H race_use marital_status_indic1)
sort H race_use marital_status_indic1 age


preserve
keep if race_use==1
drop race_use
sort H  marital_status_indic1 age
drop H  marital_status_indic1 age
outsheet using "${data_model}/Blacks\parameters_survival.txt", nolabel nonames replace	
restore

keep if race_use==3
drop race_use
sort H  marital_status_indic1 age
drop H  marital_status_indic1 age
outsheet using "${data_model}/Hispanics\parameters_survival.txt", nolabel nonames replace	

}